package com.tbvtc.agriculturalcreditchainserver2025.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tbvtc.agriculturalcreditchainserver2025.entity.DailyScore;
import com.tbvtc.agriculturalcreditchainserver2025.entity.EnvironmentData;
import com.tbvtc.agriculturalcreditchainserver2025.mapper.EnvironmentDataMapper;
import com.tbvtc.agriculturalcreditchainserver2025.service.IDailyScoreService;
import com.tbvtc.agriculturalcreditchainserver2025.service.IEnvironmentDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbvtc.agriculturalcreditchainserver2025.vo.Env.EnvScoreVo;
import com.tbvtc.agriculturalcreditchainserver2025.vo.Env.EnvTemVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * <p>
 * 温湿度光照监测数据表 服务实现类
 * </p>
 *
 * @author syh
 * @since 2025-07-21
 */
@Service
public class EnvironmentDataServiceImpl extends ServiceImpl<EnvironmentDataMapper, EnvironmentData> implements IEnvironmentDataService {
  @Resource
  private IDailyScoreService DailyScoreMapper;
  @Resource
  private IEnvironmentDataService environmentDataService;

    @Override
    public EnvTemVo getData(LocalDate date) {
        List<LocalDateTime> timeList = List.of(
                date.atTime(2, 0),    // 2点
                date.atTime(6, 0),    // 6点
                date.atTime(10, 0),   // 10点
                date.atTime(14, 0),   // 14点
                date.atTime(18, 0),   // 18点
                date.atTime(22, 0)
        );
        List<Double> amountList = new ArrayList<>();
        timeList.forEach(time -> {
            EnvironmentData data = environmentDataService.getOne(new LambdaQueryWrapper<EnvironmentData>()
                    .between(EnvironmentData::getMonitorTime, time, time.plusMinutes(1))
            );
            if (data != null){
                amountList.add(data.getTemperature());
            }else{
                amountList.add(0.0);
            }

        });
        String dateList = StringUtils.join(timeList, ",");// 输出: A,B,C
        String turnoverList = StringUtils.join(amountList, ",");
        return new EnvTemVo(timeList,amountList);
    }

    @Override
    public EnvScoreVo getScore(LocalDate begin, LocalDate end) {
        LocalDate beginParse = begin;
        List<LocalDate> timeList = new ArrayList<>();
        List<Double> amountList = new ArrayList<>();
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            timeList.add(begin);
        }
       timeList.forEach(time -> {
           DailyScore scoreMapperOne = DailyScoreMapper.getOne(new LambdaQueryWrapper<DailyScore>()
                   .eq(DailyScore::getDate, time));
           amountList.add(scoreMapperOne != null ? scoreMapperOne.getScore() : 0.0);
       });
        String dateList = StringUtils.join(timeList, ",");// 输出: A,B,C
        String turnoverList = StringUtils.join(amountList, ",");
     return new EnvScoreVo(timeList,amountList);
    }

    @Override
    public EnvTemVo getHumidity(LocalDate date) {
        List<LocalDateTime> timeList = List.of(
                date.atTime(2, 0),    // 2点
                date.atTime(6, 0),    // 6点
                date.atTime(10, 0),   // 10点
                date.atTime(14, 0),   // 14点
                date.atTime(18, 0),   // 18点
                date.atTime(22, 0)
        );
        List<Double> amountList = new ArrayList<>();
        timeList.forEach(time -> {
            EnvironmentData data = environmentDataService.getOne(new LambdaQueryWrapper<EnvironmentData>()
                    .between(EnvironmentData::getMonitorTime, time, time.plusMinutes(1))
            );
            if (data != null){
                amountList.add(data.getHumidity());
            }else {
                amountList.add(0.0);
            }

        });
        String dateList = StringUtils.join(timeList, ",");// 输出: A,B,C
        String turnoverList = StringUtils.join(amountList, ",");
        return new EnvTemVo(timeList,amountList);
    }

    @Override
    public EnvTemVo getIllumination(LocalDate date) {
        List<LocalDateTime> timeList = List.of(
                date.atTime(2, 0),    // 2点
                date.atTime(6, 0),    // 6点
                date.atTime(10, 0),   // 10点
                date.atTime(14, 0),   // 14点
                date.atTime(18, 0),   // 18点
                date.atTime(22, 0)
        );
        List<Integer> amountList = new ArrayList<>();
        timeList.forEach(time -> {
            EnvironmentData data = environmentDataService.getOne(new LambdaQueryWrapper<EnvironmentData>()
                    .between(EnvironmentData::getMonitorTime, time, time.plusMinutes(1))
            );
            if (data != null){
                amountList.add(data.getIllumination());
            }else {
                amountList.add(0);
            }

        });
        String dateList = StringUtils.join(timeList, ",");// 输出: A,B,C
        String turnoverList = StringUtils.join(amountList, ",");
        return new EnvTemVo(timeList,amountList);
    }
}
